package com.java.parallel.threadBand;

import net.openhft.affinity.AffinityLock;
import net.openhft.affinity.AffinityStrategies;
import net.openhft.affinity.AffinityThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



/**
 * @author guoyu
 * @date 2022/11/29
 * @Version 1.0
 * @Description 线程上下文频繁切换测试
 */
public class SwitchThreadTest {
    // 创建一个线程池存任务
    private static final ExecutorService pool = Executors.newFixedThreadPool(4,
            new AffinityThreadFactory("bg",
                    AffinityStrategies.SAME_CORE,
                    AffinityStrategies.DIFFERENT_SOCKET,
                    AffinityStrategies.ANY));

//    private static final ExecutorService pool = Executors.newFixedThreadPool(3);

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 12; i++) {
            pool.execute(() -> {
                try {
                    Integer a;
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            Thread.sleep(200);
            System.out.println(AffinityLock.dumpLocks());
        }

    }
}
